package tt.dz.order.pojo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tt.dz.order.dao.BaseDAOUtil;
import tt.dz.order.util.OrderConstant.SendRefundStatus;

/**
 * 发送充电预约退款订单的状态
 * @author zhoutian   
 * @date 2015-11-26
 */
public class OrderRefundStatus {
	//订单号
	private String orderNo;
	//支付渠道编码 0 有贝先付 1 支付宝 2 微信支付
	private String payChannelCode;
	//发送的状态, 0：需要发送 1：正在发送 2：发送并接收回调完成
	private int status;
	//创建时间
	private Date createTime;

	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}

	public String getPayChannelCode() {
		return payChannelCode;
	}

	public void setPayChannelCode(String payChannelCode) {
		this.payChannelCode = payChannelCode;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public OrderRefundStatus() {
		this.createTime = new Date();
		this.status = SendRefundStatus.needSendNew.value();
	}
	
	public void save(){
		String sql = "insert into Order_Refund_Status(orderNo, payChannelCode, status, createTime) values(:orderNo, :payChannelCode, :status, :createTime)";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	/**
	 * 获取需要发送的订单列表（同时进行行锁）
	 * @return 订单列表
	 */
	public static List<OrderRefundStatus> getRefundInfoForSend(String payChannelCode){
		String sql = "select orderNo, payChannelCode, status, createTime from Order_Refund_Status where payChannelCode = :payChannelCode and (status = " + SendRefundStatus.needSendNew.value() + " or status = " + SendRefundStatus.needSendAgain.value() + ") for update NOWAIT";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("payChannelCode", payChannelCode);
		return BaseDAOUtil.queryForList(sql, paramMap, OrderRefundStatus.class);
	}
	
	/**
	 * 修改充电预约退款订单的状态为"正在发送"
	 * @param orderNos 订单ID列表
	 */
	@SuppressWarnings("unchecked")
	public static void changeStatusToSending(List<String> orderNos){
		String sql = "update Order_Refund_Status set status = " + SendRefundStatus.sending.value() + " where orderNo = :orderNo";
		Map<String, Object>[] batchValues = new HashMap[orderNos.size()];
		for(int i = 0 ; i < orderNos.size(); i++){
			String orderNo = orderNos.get(i);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("orderNo", orderNo);
			batchValues[i] = paramMap;
		}
		BaseDAOUtil.batchUpdate(sql, batchValues);
	}
	
	/**
	 * 修改充电预约退款订单的状态为"需要再次发送"
	 * @param orderNos 订单ID列表
	 */
	@SuppressWarnings("unchecked")
	public static void changeStatusToNeedSendAgain(List<String> orderNos){
		String sql = "update Order_Refund_Status set status = " + SendRefundStatus.needSendAgain.value() + " where orderNo = :orderNo";
		Map<String, Object>[] batchValues = new HashMap[orderNos.size()];
		for(int i = 0 ; i < orderNos.size(); i++){
			String orderNo = orderNos.get(i);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("orderNo", orderNo);
			batchValues[i] = paramMap;
		}
		BaseDAOUtil.batchUpdate(sql, batchValues);
	}
	
	/**
	 * 修改充电预约退款订单的状态为"发送成功"
	 * @param orderNos 订单ID列表
	 */
	@SuppressWarnings("unchecked")
	public static void changeStatusToSuccess(List<String> orderNos){
		String sql = "update Order_Refund_Status set status = " + SendRefundStatus.success.value() + " where orderNo = :orderNo";
		Map<String, Object>[] batchValues = new HashMap[orderNos.size()];
		for(int i = 0 ; i < orderNos.size(); i++){
			String orderNo = orderNos.get(i);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("orderNo", orderNo);
			batchValues[i] = paramMap;
		}
		BaseDAOUtil.batchUpdate(sql, batchValues);
	}
}
